package com.ruoyi.system.service.vantservice.orderservice.impl;

import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.domain.zhuangxiumain.orderformMain.OrderDetail;
import com.ruoyi.system.domain.zhuangxiumain.orderformMain.OrderMaster;
import com.ruoyi.system.domain.zhuangxiumain.orderformMain.OrderShoppingCart;
import com.ruoyi.system.domain.zhuangxiumain.orderformMain.OrderVoucher;
import com.ruoyi.system.domain.zhuangxiumain.vo.ShoppingCartVo;
import com.ruoyi.system.domain.zhuangxiumain.vo.ShoppingOrderVo;
import com.ruoyi.system.domain.zhuangxiumain.vo.ShoppingPaymentVo;
import com.ruoyi.system.mapper.vant.order.ApiOrderDetailMapper;
import com.ruoyi.system.mapper.vant.order.ApiOrderMasterMapper;
import com.ruoyi.system.mapper.vant.order.ApiOrderShoppingCartMapper;
import com.ruoyi.system.mapper.vant.order.ApiOrderVoucherMapper;
import com.ruoyi.system.service.vantservice.orderservice.ApiOrderShoppingCartService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by 惠普 on 2021/1/11 14:50
 */
@Service("apiOrderShoppingCartService")
public class ApiOrderShoppingCartServiceImpl implements ApiOrderShoppingCartService {
    @Resource
    private ApiOrderShoppingCartMapper apiOrderShoppingCartMapper;
    @Resource
    private ApiOrderMasterMapper apiOrderMasterMapper;
    @Resource
    private ApiOrderDetailMapper apiOrderDetailMapper;
    @Autowired
    private ApiOrderVoucherMapper apiOrderVoucherMapper;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private HttpServletRequest request;

    /**
     * 添加购物车信息
     * @param orderShoppingCart
     * @return
     */
    @Override
    public int addOrderShoppingCart(OrderShoppingCart orderShoppingCart) {
        int row = 0;
        // 获取前台登录用户信息
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        orderShoppingCart.setCustomerId(Integer.parseInt(String.valueOf(customerLogin.getCustomerId())));
        orderShoppingCart.setAddTime(new Date());
        orderShoppingCart.setUpdateTime(new Date());
        row = apiOrderShoppingCartMapper.addOrderShoppingCart(orderShoppingCart);
        return row;
    }

    /**
     * 查询购物车列表
     * @return
     */
    @Override
    public List<ShoppingCartVo> selectOrderShoppingCart() {
        // 获取前台登录用户信息
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        return apiOrderShoppingCartMapper.selectOrderShoppingCart(Integer.parseInt(String.valueOf(customerLogin.getCustomerId())));
    }

    /**
     * 删除购物车信息
     * @param orderShoppingCartId
     * @return
     */
    @Override
    public int deleteOrderShoppingCart(Integer orderShoppingCartId) {
        return apiOrderShoppingCartMapper.deleteOrderShoppingCart(orderShoppingCartId);
    }

    /**
     * 添加订单表
     * @param shoppingOrderVo
     * @return
     */
    @Override
    public List addOrder(ShoppingOrderVo shoppingOrderVo) {
        List list = new ArrayList();
        int row = 0;
        for (ShoppingCartVo shoppingCartVo : shoppingOrderVo.getResultList()){
            OrderMaster orderMaster = new OrderMaster();
            OrderDetail orderDetail = new OrderDetail();
            orderMaster.setCustomerId(shoppingCartVo.getCustomerId());
            orderMaster.setCustomerPhone(shoppingOrderVo.getTel());
            orderMaster.setShippingUser(shoppingOrderVo.getName());
            orderMaster.setProvince(shoppingOrderVo.getProvince());
            orderMaster.setCity(shoppingOrderVo.getCity());
            orderMaster.setDistrict(shoppingOrderVo.getCounty());
            orderMaster.setAddress(shoppingOrderVo.getAddressDetail());
            orderMaster.setOrderMoney(shoppingCartVo.getLabelPrice());
            if(shoppingCartVo.getLabelFreight()!=null){
                orderMaster.setShippingMoney(shoppingCartVo.getLabelFreight());
            }
            orderMaster.setPaymentMoney(shoppingCartVo.getLabelPrice()*shoppingCartVo.getProductNumber());
            orderMaster.setCreateTime(new Date());
            orderMaster.setInvoiceTime(shoppingOrderVo.getOrderPaper());
            orderMaster.setSupplierId(shoppingCartVo.getSupplierId());

            row = apiOrderMasterMapper.insertOrderMaster(orderMaster);
            if(row == 1){
                list.add(orderMaster.getOrderId());
                orderDetail.setOrderId(orderMaster.getOrderId());
                orderDetail.setProductId(shoppingCartVo.getProductId());
                orderDetail.setProductName(shoppingCartVo.getProductName());
                orderDetail.setProductCnt(shoppingCartVo.getProductNumber());
                orderDetail.setProductPrice(shoppingCartVo.getLabelPrice());
                orderDetail.setLabelId(shoppingCartVo.getProductLabelId());
                row = apiOrderDetailMapper.insertOrderDetail(orderDetail);
                if(row == 1){
                    row = apiOrderShoppingCartMapper.deleteOrderShoppingCart(shoppingCartVo.getOrderShoppingCartId());
                    if(row == 0){
                        return new ArrayList();
                    }
                }else {
                    return new ArrayList();
                }
            }else {
                return new ArrayList();
            }
        }
        return list;
    }

    /**
     * 购物车线下支付
     * @param shoppingPaymentVo
     * @return
     */
    @Override
    public int shoppingPayment(ShoppingPaymentVo shoppingPaymentVo) {
        int row = 0;
        for (Integer integer : shoppingPaymentVo.getList()){
            OrderMaster orderMaster = new OrderMaster();
            orderMaster.setOrderId(integer);
            orderMaster.setPayTime(new Date());
            orderMaster.setOrderStatus(10);
            orderMaster.setPaymentMethod(2);
            StringBuilder stringBuilder = new StringBuilder();
            Random random = new Random();
            for (int i=0;i<8;i++){
                stringBuilder.append(random.nextInt(10));
            }
            String num =stringBuilder.toString();
            orderMaster.setOrderSn(num);


            row = apiOrderMasterMapper.updateOrderMethodPing(orderMaster);
            if(row == 1){
                OrderVoucher orderVoucher = new OrderVoucher();
                orderVoucher.setOrderMasterId(Long.parseLong(String.valueOf(integer)));
                orderVoucher.setOrderVoucherUrl(shoppingPaymentVo.getUrl());
                row = apiOrderVoucherMapper.insertOrderVoucher(orderVoucher);
            }else {
                return 0;
            }
        }
        return row;
    }

    /**
     * 购物车微信支付
     * @param shoppingPaymentVo
     * @return
     */
    @Override
    public int shoppingPaymentWx(ShoppingPaymentVo shoppingPaymentVo) {
        int row = 0;
        for (Integer integer : shoppingPaymentVo.getList()){
            OrderMaster orderMaster = new OrderMaster();
            orderMaster.setOrderId(integer);
            orderMaster.setPayTime(new Date());
            orderMaster.setOrderStatus(2);
            row = apiOrderMasterMapper.updateOrderMethod(orderMaster);
        }
        return row;
    }
}
